package com.shengruan.common;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class CVSJdbc {
	
	private String databaseURL = "jdbc:relique:csv:"; // 是数据库的存放位置

	private ResultSet rs = null;

	private Connection connect = null;

	private Statement statement = null;

	private String dbDriver = "org.relique.jdbc.csv.CsvDriver";


	public CVSJdbc(String path) {
		Properties p = new Properties();
        p.put("charset", "GBK");
      
		try {
			Class.forName(dbDriver); // derby数据库的embbed驱动
			connect = DriverManager.getConnection(databaseURL + path,p);
		} catch (Exception e) {
			// System.out.print("驱动载入错误!");
			e.printStackTrace();
		}
	}

	public ResultSet executeQuery(String sql) {
		try {
			//connect = DriverManager.getConnection(databaseURL + path);
			Statement s = connect.createStatement();
			rs = s.executeQuery(sql);
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
		return rs;
	}

	public int executeUpdate(String sql) {
		int result = 0;
		try {
			statement = connect.createStatement();
			result = statement.executeUpdate(sql);
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
		return result;
	}

	public void closeDB() {
		try {
			if(rs!=null)
				rs.close();
		} catch (Exception ex) {

		}
		try {
			if(statement!=null)
				statement.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		try {
			if(connect!=null)
				connect.close();
		} catch (Exception ex) {
			ex.printStackTrace();

		}
	}
}
